package com.itheima.web.task;


import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.domain.cargo.Contract;
import com.itheima.domain.cargo.ContractExample;
import com.itheima.domain.cargo.ExportProduct;
import com.itheima.service.cargo.ContractService;
import com.itheima.web.utils.DateUtils;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;

import java.text.SimpleDateFormat;
import java.util.*;

public class MyTask {
    @Reference(timeout = 2)
    private ContractService contractService;
    @Autowired
    private RabbitTemplate rabbitTemplate;
    public void excete() {
        System.out.println("当前时间为："+
                new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date()));
    }

    public void checkShipTime(){
        System.out.println("定时任务开始"+new Date());
        //查找符合的数据(co_contract ids)  船期到期  当前日期 日+3=交货期 时间范围（between and ） 今天开始时间+3 - 今日结束时间+3
        // 构造查询对象
        ContractExample example = new ContractExample();
        // 根据ship_time 交货时间，升序排序
        example.setOrderByClause("ship_time asc");
        // 条件集合
        ContractExample.Criteria criteria = example.createCriteria();
        //今日开始时间
        String startTime =DateUtils.getDifOfDay(DateUtils.getStartOfDay(new Date()),2);
        //今日结束时间
        String endTime =DateUtils.getDifOfDay(DateUtils.getEndOfDay(new Date()),2);
        System.out.println("开始时间为"+startTime);
        System.out.println("结束时间为"+endTime);
        Date startDate=DateUtils.stringToDate(startTime);
        Date endDate=DateUtils.stringToDate(endTime);
        criteria.andShipTimeBetween(startDate,endDate);

        //合并 ids 发送rabbitmq
        Map<String, String> idList = new HashMap<>();

        String ids ="";
/*        List<Contract> contractList = contractService.findAll(example);
        if (contractList != null && contractList.size() > 0) {
            Integer number=0;
            for (Contract contract : contractList) {
                if(number<contractList.size()-1) {
                    ids+=contract.getId()+",";
                }else{
                    ids+=contract.getId();
                }
                number++;
            }
        }*/

        for (int i = 0; i <2 ; i++) {
            if(i==0)ids="029c2588-7823-4cb9-b0fb-c69b6731da90,";
            if(i==1)ids+="03d2e074-96fe-4b59-836f-1743e0777b56";
        }

        idList.put("ids",ids);
        // 发送消息
        rabbitTemplate.convertAndSend("myExchange","msg.shipTime",idList);
    }

}
